import { drawerInputValidators, drawerInstance, drawerList } from "./store";
import { handleSubmitApiTask } from "./utils";
import { tmp_proxy } from ".";
import { DrawerFormStateType } from "./type";
export const handleSubmit = () => handleSubmitApiTask();
export const drawer_disable_ok_button = computed(() => {
  return (
    isEmptyType(drawerFormState.name) ||
    isEmptyType(drawerFormState.username) ||
    isEmptyType(drawerFormState.start_end) ||
    isEmptyType(drawerFormState.status) ||
    drawerInputValidators.some((bool) => bool === true)
  );
});
export const drawerLoading = ref(false);
export const drawerVisible = ref(false);
export const drawerFormState = reactive<DrawerFormStateType>({
  id: undefined,
  name: undefined,
  username: undefined,
  start_end: [],
  status: 0,
  password: undefined,
  password_confirmation: undefined,
});
export const drawerTitle = ref("");
export const drawerConfig = () => {
  const defineDrawer = {
    drawerInstance,
    loading: drawerLoading,
    visible: drawerVisible,
    title: drawerTitle,
    maskClosable: true,
    disable_ok_button: drawer_disable_ok_button,
    formState: drawerFormState,
    drawerList,
    getContainer: "#app",
    slot: {
      extra: () => {
        const loading = drawerLoading;
        return (
          <>
            <a-space size={8}>
              <s-button
                icon={renderIcon("icon-cancel")}
                onClick={() =>
                  tmp_proxy.$confirm({
                    closable: true,
                    maskClosable: true,
                    icon: null,
                    okText: $t("base.ok"),
                    cancelText: $t("base.cancel"),
                    title: () => <>{$t("base.prompt")}</>,
                    style: {
                      top: "30%",
                    },
                    content: <>{$t("base.cancelTips")}</>,
                    onOk: () => (drawerVisible.value = false),
                  })
                }
              >
                {$t("base.cancel")}
              </s-button>
              <s-button
                icon={renderIcon("icon-tubiao_tijiao")}
                loading={loading.value}
                disabled={drawer_disable_ok_button.value}
                type="primary"
                onClick={async () => handleSubmit()}
              >
                {$t("base.save")}
              </s-button>
            </a-space>
          </>
        );
      },
    },
  };
  return defineDrawer;
};
